/*
 * Copyright (C) 2018 Pingan, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.dashboard.dto;

import java.util.Date;
import java.util.Objects;

/**
 * @version 1.0
 * @description es 请求json参数 dto
 * @auther baojimin
 * @create 2018/4/18 13:57
 */
public class EsJsonParamDto {

    private Date fromTime;

    private Date toTime;

    public EsJsonParamDto(Date fromTime, Date toTime) {
        this.fromTime = fromTime;
        this.toTime = toTime;
    }

    public String toJsonParam() {
        Objects.requireNonNull(fromTime, " from time can not be null");
        Objects.requireNonNull(toTime, "to time can not be null");
        return "{" +
                "\"query\": {" +
                "\"bool\": {" +
                "\"must\": [{\"match_all\": {}}," +
                "           {\"range\": {\"@timestamp\": {\"gte\": " + fromTime.getTime() + ",\"lte\":" + toTime.getTime() + ",\"format\": \"epoch_millis\"}}}" +
                "]," +
                "\"must_not\": []" +
                "}" +
                "}," +
                "\"size\": 0," +
                "\"_source\": {" +
                "\"excludes\": []" +
                "}," +
                "\"aggs\": {" +
                "\"esAgg\": {" +
                "\"terms\": {" +
                "\"field\": \"sorl_collection.keyword\"," +
                "\"size\": 100," +
                "\"order\": {" +
                "\"_count\": \"desc\"" +
                "}" +
                "}," +
                "\"aggs\": {" +
                "\"avgResponseTime\": {\"avg\": {\"field\": \"response_time\"}}," +
                "\"minResponseTime\": {\"min\": {\"field\": \"response_time\"}}," +
                "\"maxResponseTime\": {\"max\": {\"field\": \"response_time\"}}," +
                "\"count0_\": {\"avg_bucket\": {\"buckets_path\": \"5-bucket>_count\"}}," +
                "\"count0_100\": {\"avg_bucket\": {\"buckets_path\": \"6-bucket>_count\"}}," +
                "\"count100_1000\": {" +
                "\"avg_bucket\": {\"buckets_path\": \"7-bucket>_count\"}" +
                "}," +
                "\"count1000_3000\": {" +
                "\"avg_bucket\": {\"buckets_path\": \"8-bucket>_count\"}" +
                "}," +
                "\"count3000_\": {" +
                "\"avg_bucket\": {" +
                "\"buckets_path\": \"9-bucket>_count\"" +
                "}" +
                "}," +
                "\"5-bucket\": {" +
                "\"range\": {" +
                "\"field\": \"response_time\"," +
                "\"ranges\": [{" +
                "\"from\": 0" +
                "}]," +
                "\"keyed\": true" +
                "}" +
                "}," +
                "\"6-bucket\": {" +
                "\"range\": {" +
                "\"field\": \"response_time\"," +
                "\"ranges\": [{" +
                "\"from\": 0," +
                "\"to\": 100" +
                "}]," +
                "\"keyed\": true" +
                "}" +
                "}," +
                "\"7-bucket\": {" +
                "\"range\": {" +
                "\"field\": \"response_time\"," +
                "\"ranges\": [{" +
                "\"from\": 100," +
                "\"to\": 1000" +
                "}]," +
                "\"keyed\": true" +
                "}" +
                "}," +
                "\"8-bucket\": {" +
                "\"range\": {" +
                "\"field\": \"response_time\"," +
                "\"ranges\": [{" +
                "\"from\": 1000," +
                "\"to\": 3000" +
                "}]," +
                "\"keyed\": true" +
                "}" +
                "}," +
                "\"9-bucket\": {" +
                "\"range\": {" +
                "\"field\": \"response_time\"," +
                "\"ranges\": [{" +
                "\"from\": 3000," +
                "\"to\": 10000000" +
                "}]," +
                "\"keyed\": true" +
                "}" +
                "}" +
                "}" +
                "}" +
                "}" +
                "}";
    }


}
